package com.platform.controller;

import java.util.List;
import java.util.Map;

import com.platform.entity.AgentEntity;
import com.platform.service.AgentService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.platform.entity.AgentGoodsEntity;
import com.platform.service.AgentGoodsService;
import com.platform.utils.PageUtils;
import com.platform.utils.Query;
import com.platform.utils.R;

/**
 * Controller
 *
 * @author lipengjun
 * @email 939961241@qq.com
 * @date 2018-10-07 20:42:59
 */
@RestController
@RequestMapping("agentgoods")
public class AgentGoodsController extends AbstractController {
    @Autowired
    private AgentGoodsService agentGoodsService;
    @Autowired
    private AgentService agentService;

    /**
     *
     * @param agent
     * @return
     */
    @RequestMapping("/addGoodsToMyShop")
    public R addGoodsToMyShop(@RequestBody AgentEntity agent){
        if(agent != null){

            if(agent.getAgentGoodsIds() == null || agent.getAgentGoodsIds().trim().length() == 0){
                return R.error("提交的代理商品为空");
            }

            List<AgentEntity> agentList = agentService.queryExistAgentApply(agent.getVendorRootDeptId(), getRootDept().getDeptId());
            if(agentList == null || agentList.size() > 1){
                return R.error("检测到错误的代理关系数据");
            }

            AgentGoodsEntity entity = agentGoodsService.addGoodsToMyShop(agentList.get(0), agent.getAgentGoodsIds(), getUserId());
            return R.ok();
        }
        else{
            return R.error();
        }
    }
    /**
     * 查看列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("agentgoods:list")
    public R list(@RequestParam Map<String, Object> params) {
        //查询列表数据
        Query query = new Query(params);

        List<AgentGoodsEntity> agentGoodsList = agentGoodsService.queryList(query);
        int total = agentGoodsService.queryTotal(query);

        PageUtils pageUtil = new PageUtils(agentGoodsList, total, query.getLimit(), query.getPage());

        return R.ok().put("page", pageUtil);
    }

    /**
     * 查看信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("agentgoods:info")
    public R info(@PathVariable("id") Integer id) {
        AgentGoodsEntity agentGoods = agentGoodsService.queryObject(id);

        return R.ok().put("agentGoods", agentGoods);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("agentgoods:save")
    public R save(@RequestBody AgentGoodsEntity agentGoods) {
        agentGoodsService.save(agentGoods);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("agentgoods:update")
    public R update(@RequestBody AgentGoodsEntity agentGoods) {
        agentGoodsService.update(agentGoods);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("agentgoods:delete")
    public R delete(@RequestBody Integer[] ids) {
        agentGoodsService.deleteBatch(ids);

        return R.ok();
    }

    /**
     * 查看所有列表
     */
    @RequestMapping("/queryAll")
    public R queryAll(@RequestParam Map<String, Object> params) {

        List<AgentGoodsEntity> list = agentGoodsService.queryList(params);

        return R.ok().put("list", list);
    }
}
